package com.springboot.wisdom.email.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.springboot.wisdom.common.common.bean.MyResult;
import com.springboot.wisdom.email.service.EmailService;
import com.springboot.wisdom.serviceApi.email.model.vo.EmailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;

/**
 * @Description
 * @Author LY L
 * @Date 2024-12-23
 */
@Api(tags = "邮箱信息")
@ApiSupport(author = "LY L")
@RestController
@RequestMapping(value = "/email", produces = {"application/json;charset=UTF-8"})
@Slf4j
@Validated     // springboot 2.5.* 版本需要在类上标注 @Validated 注解校验注解才会生效。
public class EmailController {

    @Resource
    private EmailService emailService;


    /*************************************************** 邮箱操作功能 ***************************************************/

    /**
     * 发送邮箱。
     */
    @ApiOperation(value = "发送邮箱", notes = "")
    @PostMapping("/sendEmail")
    public MyResult<String> sendAttachmentsMail(@RequestBody @Validated EmailVO emailVO) {
        return emailService.sendAttachmentsMail(emailVO);
    }




    /*************************************************** 邮箱获取功能 ***************************************************/

    @ApiOperation(value = "邮箱验证码验证", notes = "校验邮箱验证码是否正确")
    @GetMapping("/get/email/check")
    public MyResult<Boolean> checkEmail(
            @RequestParam @NotBlank(message = "邮箱不能为空") @Email(message = "邮箱格式不正确！") String email,
            @RequestParam @NotBlank(message = "验证码不能为空") String code
    ) {
        return emailService.checkEmail(email, code);
    }

    /**
     * 获取邮箱验证码。有效期：5分钟。
     */
    @ApiOperation(value = "获取邮箱验证码", notes = "")
    @GetMapping("/get/code")
    public MyResult<String> getEmailCode(@RequestParam @NotBlank(message = "邮箱不能为空") @Email(message = "邮箱格式不正确！") String email) {
        return emailService.getEmailCode(email);
    }

}
